﻿using System;
using System.IO;
using System.Windows.Media.Imaging;

namespace Uinside.CreditSystem.WPF.Tools
{
    public class BinaryJPEGToImageSourceConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var binaryimagedata = value as byte[];
            Stream imageStreamSource = new MemoryStream(binaryimagedata, false);
            var decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            BitmapSource bitmapSource = decoder.Frames[0];
            imageStreamSource.Close();
            return bitmapSource;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
